package cn.com.kh;

import cn.com.elite.EliteBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @projectName: ngsapitask-hst
 * @package: cn.com.kh
 * @className: AllotUtil
 * @author: LiJT
 * @description: TODO
 * @date: 2022-12-19 14:03
 * @version: 1.0
 */
public class AllotUtil {
    public static void main(String[] args) throws Exception {
        // 获取数据源
        //JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());

/*
        Map<String, String> userp = new HashMap<>();
        userp.put("张三","30");
        userp.put("李四","30");
        userp.put("王五","40");
        List<List<String>> resUsers = getAllotNum(userp, 1000, "权重");
        System.err.println(resUsers);
*/

        List stafflist2 = new ArrayList();
        stafflist2.add("张三");
        stafflist2.add("王五");
        List<List<String>> resUsers = getAllotNum(stafflist2, 1, "平均");
        System.err.println(resUsers);


    }

    public static List<List<String>> getAllotNum(Map<String, String> users, int tasknum, String allotType) {
        List<List<String>> resUsers = new ArrayList<>();
        // 数据准备
        List<String> tasks = new ArrayList<>();
        for (int i = 1; i <= tasknum; i++) {
            tasks.add(String.valueOf(i));
        }
        users = sortByValue(users);
        if (allotType.equals("权重")) {
            Map<String, List<String>> res = allotOfProportion(users, tasks);
            Iterator<Map.Entry<String, List<String>>> it = res.entrySet().iterator();
            while (it.hasNext()) {
                List<String> nowUsers = new ArrayList<>();
                Map.Entry<String, List<String>> entry = it.next();
                nowUsers.add(entry.getKey());
                nowUsers.add(String.valueOf(entry.getValue().size()));
                resUsers.add(nowUsers);
            }
        }
        return resUsers;
    }

    public static List<List<String>> getAllotNum(List<String> users, int tasknum, String allotType) {
        List<List<String>> resUsers = new ArrayList<>();
        // 数据准备
        List<String> tasks = new ArrayList<>();
        for (int i = 1; i <= tasknum; i++) {
            tasks.add(String.valueOf(i));
        }
        if (allotType.equals("平均")) {
            Map<String, List<String>> res = allotOfAverage(users, tasks);
            Iterator<Map.Entry<String, List<String>>> it = res.entrySet().iterator();
            while (it.hasNext()) {
                List<String> nowUsers = new ArrayList<>();
                Map.Entry<String, List<String>> entry = it.next();
                nowUsers.add(entry.getKey());
                nowUsers.add(String.valueOf(entry.getValue().size()));
                resUsers.add(nowUsers);
            }
        }
        return resUsers;
    }
    /*
     * 随机分配
     */
    public Map<String, List<String>> allotOfRandom(List<String> users, List<String> tasks) {
        Map<String, List<String>> allot = new ConcurrentHashMap<String, List<String>>(); //保存分配的信息
        if (users != null && users.size() > 0 && tasks != null && tasks.size() > 0) {
            for (int i = 0; i < tasks.size(); i++) {
                int r_user = new Random().nextInt(users.size());
                if (allot.containsKey(users.get(r_user))) {
                    List<String> list = allot.get(users.get(r_user));
                    list.add(tasks.get(i));
                    allot.put(users.get(r_user), list);
                } else {
                    List<String> list = new ArrayList<String>();
                    list.add(tasks.get(i));
                    allot.put(users.get(r_user), list);
                }
            }
        }
        return allot;
    }
    /*
     * 平均分配
     */
    public static Map<String, List<String>> allotOfAverage(List<String> users, List<String> tasks) {
        Map<String, List<String>> allot = new ConcurrentHashMap<String, List<String>>(); //保存分配的信息
        if (users != null && users.size() > 0 && tasks != null && tasks.size() > 0) {
            for (int i = 0; i < tasks.size(); i++) {
                int j = i % users.size();
                if (allot.containsKey(users.get(j))) {
                    List<String> list = allot.get(users.get(j));
                    list.add(tasks.get(i));
                    allot.put(users.get(j), list);
                } else {
                    List<String> list = new ArrayList<String>();
                    list.add(tasks.get(i));
                    allot.put(users.get(j), list);
                }
            }
        }
        return allot;
    }

    /*
     * 权重分配
     */
    public static Map<String, List<String>> allotOfProportion(Map<String, String> users, List<String> tasks) {
        Map<String, List<String>> allot = new ConcurrentHashMap<String, List<String>>(); //保存分配的信息
        if (users != null && users.size() > 0 && tasks != null && tasks.size() > 0) {
            int a = 0;//总权重
            for (Map.Entry<String, String> entry : users.entrySet()) {
                a += Integer.parseInt(entry.getValue());
            }
            int start = 0, end = 0;//起始下标 ,结束下标
            if (a > 0) {
                for (Map.Entry<String, String> entry : users.entrySet()) {
                    List<String> allotTask = new ArrayList<String>();
                    end += Integer.parseInt(entry.getValue());//权重累计
                    for (; start < tasks.size() * end / a; start++) {
                        allotTask.add(tasks.get(start));
                    }
                    allot.put(entry.getKey(), allotTask);
                }
            }
        }
        return allot;
    }

    // 对Map排序
    public static Map<String, String> sortByValue(Map<String, String> oriMap) {
        if (oriMap == null || oriMap.isEmpty()) {
            return null;
        }

        Map<String, String> sortedMap = new LinkedHashMap<>();
        List<Map.Entry<String, String>> entryList = new ArrayList<>(oriMap.entrySet());
        Collections.sort(entryList, (me1, me2) -> me1.getValue().compareTo(me2.getValue()));

        Iterator<Map.Entry<String, String>> iterator = entryList.iterator();
        Map.Entry<String, String> tmpEntry;
        while (iterator.hasNext()) {
            tmpEntry = iterator.next();
            sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
        }
        return sortedMap;
    }

    //分配任务
    public static boolean assignTasks(EliteBean eliteBean, String Rule_Id) {
        boolean reFlag = true;



        return reFlag;
    }

}
